﻿var environment = environment ||
{
    getVirtualHostInfoes: window.prefixURL + "VirtualHost/GetVirtualHostInfoes",
    getVirtualHostInfo: window.prefixURL + "VirtualHost/GetVirtualHostInfo",
    delVirtualHostInfo: window.prefixURL + "VirtualHost/DeleteVirtualHostInfo",
    addOrUpdateVirtualHostInfo: window.prefixURL + "VirtualHost/AddOrUpdateVirtualHostInfo",

    bodyTpl: _.template($("#listTemplate").html()),
    pagerTpl: _.template($("#pagingTablePager").html()),

    pageIndex: function () {
        return $('#nav').find('li[flag]').find('a').html();
    },
    pageSize: function () {
        return $('#nav').find('.pagingTablePageCountSelect').val();
    },

    getList: function (pageIndex) {
        var t = this;
        var url = t.getVirtualHostInfoes + "?pageIndex=" + pageIndex + "&pageSize=10";

        var assetId = $("#s_AssetId").val();
        var area = $("#s_Area").val();
        var deviceType = $("#s_DeviceType").val();
        var os = $("#s_OS").val();
        var innerIP = $("#s_InnerIP").val();
        var outerIP = $("#s_OuterIP").val();
        var hardDiskSize = $("#s_HardDiskSize").val();
        var ram = $("#s_RAM").val();
        var cpu = $("#s_CPU").val();
        var status = $("#s_Status").val();
        var expireTimeStart = $("#s_ExpireTimeStart").val();
        var expireTimeEnd = $("#s_ExpireTimeEnd").val();

        $.ajax({
            url: url,
            type: "POST",
            dataType: "Json",
            data: {
                AssetId: assetId,
                Area: area,
                DeviceType: deviceType,
                OS: os,
                InnerIP: innerIP,
                OuterIP: outerIP,
                CPU: cpu,
                HardDiskSize: hardDiskSize,
                RAM: ram,
                Status: status,
                ExpireTimeStart: expireTimeStart,
                ExpireTimeEnd: expireTimeEnd
            },
            success: function (data) {
                if (data != null) {
                    $("#tbody").html(t.bodyTpl(data.returnObj));
                    $("#nav").html(t.pagerTpl(data.pager));
                    $(".pagingTablePageCountSelect").val(data.pager.PageSize);
                }
            },
            error: function (data) {

            }
        });
    },

    del: function (sender, id) {
        var returnVal = confirm("Are you sure？");
        if (returnVal === false) {
            return;
        } else {
            $.ajax({
                url: sender.delVirtualHostInfo + "?id=" + id,
                type: "GET",
                dataType: "Json",
                success: function (data) {
                    if (data.returnObj === true) {
                        alert("Success！");
                        sender.getList(1);
                    } else {
                        alert("Error:" + data.businessMessage);
                    }
                }
            });
        }
    },

    edit: function (sender, id) {
        $.ajax({
            url: sender.getVirtualHostInfo + "?id=" + id,
            type: "GET",
            dataType: "Json",
            success: function (data) {
                if (data.businessMessage !== -1) {
                    $("#Id").val(data.returnObj.id);
                    $("#txtAssetId").val(data.returnObj.assetId);
                    $("#txtArea").val(data.returnObj.area);
                    $("#txtUserName").val(data.returnObj.adminName);
                    $("#txtPassword").val(data.returnObj.password);
                    $("#txtInnerIP").val(data.returnObj.innerIP);
                    $("#txtOuterIP").val(data.returnObj.outerIP);
                    $("#txtCPU").val(data.returnObj.cpu);
                    $("#txtHardDiskSize").val(data.returnObj.hardDiskSize);
                    $("#txtRAM").val(data.returnObj.ram);
                    $("#txtNetworkBandwidth").val(data.returnObj.networkBandwidth);
                    $("#txtMaintainers").val(data.returnObj.maintainers);
                    $("#txtExpireTime").val(data.returnObj.expireTime);
                    $("#txtRemark").val(data.returnObj.remark);
                    $("#createModel").modal();
                } else {
                    alert(data.businessMessage);
                }
            },
            error: function (data) {
                alert("Error:" + data);
            }
        });
    },

    detail: function (sender, id) {
        $.ajax({
            url: sender.getVirtualHostInfo + "?id=" + id,
            type: "GET",
            dataType: "Json",
            success: function (data) {
                if (data.businessMessage !== -1) {
                    $("#labelAssetId").text(data.returnObj.assetId);
                    $("#labelArea").text(data.returnObj.area);
                    $("#labelOS").text(data.returnObj.os);
                    $("#labelOSVersion").text(data.returnObj.osVersion);
                    $("#labelUserName").text(data.returnObj.adminName);
                    $("#labelPassword").text(data.returnObj.password);
                    $("#labelInnerIP").text(data.returnObj.innerIP);
                    $("#labelOuterIP").text(data.returnObj.outerIP);
                    $("#labelCPU").text(data.returnObj.cpu);
                    $("#labelHardDiskSize").text(data.returnObj.hardDiskSize);
                    $("#labelRAM").text(data.returnObj.ram);
                    $("#labelNetworkBandwidth").text(data.returnObj.networkBandwidth);
                    $("#labelMaintainers").text(data.returnObj.maintainers);
                    $("#labelCreateTime").text(data.returnObj.createTime);
                    $("#labelExpireTime").text(data.returnObj.expireTime);
                    $("#labelRemark").text(data.returnObj.remark); 
                    $("#detailModel").modal();
                } else {
                    alert(data.businessMessage);
                }
            },
            error: function (data) {
                alert("Error:" + data);
            }
        });
    },

    initPage: function () {
        var t = this;
        t.getList(1);
    },

    bindEvents: function () {
        var t = this;

        $("span[name='opt-create']").bind("click", function () {
            $("#Id").val("");
            $("#txtAssetId").val("");
            $("#txtArea").val("");
            $("#txtUserName").val("");
            $("#txtPassword").val("");
            $("#txtInnerIP").val("");
            $("#txtOuterIP").val("");
            $("#txtCPU").val("");
            $("#txtHardDiskSize").val("");
            $("#txtRAM").val("");
            $("#txtNetworkBandwidth").val("");
            $("#txtMaintainers").val("");
            $("#txtExpireTime").val("");
            $("#txtRemark").val("");
            $("#createModel").modal();
        });

        $("#tbody").delegate("tr td span", "click", function () {
            var type = $(this).data("type");
            var id = $(this).data("id");
            if (type === "edit") {
                t.edit(t, id);
            } else if (type === "detail") {
                t.detail(t, id);
            }else {
                t.del(t, id);
            }
        });

        $("#btnSubmit").bind("click", function () {

            var $btn = $(this).button("loading...");
            var id = $("#Id").val();
            var assetId = $("#txtAssetId").val();
            var area = $("#txtArea").val();
            var userName = $("#txtUserName").val();
            var password = $("#txtPassword").val();
            var innerIP = $("#txtInnerIP").val();
            var outerIP = $("#txtOuterIP").val();
            var cpu = $("#txtCPU").val();
            var hardDiskSize = $("#txtHardDiskSize").val();
            var ram = $("#txtRAM").val();
            var networkBandwidth = $("#txtNetworkBandwidth").val();
            var maintainers = $("#txtMaintainers").val();
            var expireTime = $("#txtExpireTime").val();
            var remark = $("#txtRemark").val();

            $.ajax({
                url: t.addOrUpdateVirtualHostInfo,
                type: "POST",
                dataType: "Json",
                data: {
                    Id: id,
                    AssetId: assetId,
                    Area: area,
                    AdminName: userName,
                    Password: password,
                    InnerIP: innerIP,
                    OuterIP: outerIP,
                    CPU: cpu,
                    HardDisksize: hardDiskSize,
                    RAM: ram,
                    NetworkBandwidth: networkBandwidth,
                    Maintainers: maintainers,
                    ExpireTime: expireTime,
                    Remark: remark
                },
                success: function (data) {
                    if (data.returnObj === true) {
                        $("#createModel").modal('hide');
                        $btn.button('reset');
                        t.getList(1);
                    } else {
                        $("#createErrorMsg").show().children("div").find("span").html(data.businessMessage);
                        $btn.button('reset');
                    }
                },
                error: function (event) {
                    alert("Error:" + event);
                }
            });
        });

        $("#nav").delegate("ul li:not('.disabled')", 'click', function () {
            var arialabel = $(this).children('a').attr('aria-label');
            var pageIndex = $(this).children('a').attr('data-page');
            var activePageIndex = t.pageIndex();
            if (arialabel != undefined) {
                if (arialabel === 'Previous') {
                    t.getList(parseInt(activePageIndex) - 1);
                } else if (arialabel === 'Next') {
                    t.getList(parseInt(activePageIndex) + 1);
                }
            } else {
                t.getList(pageIndex);
            }
        });

        $("#search-submit").bind("click", function () {
            t.getList(1);
        });

        $("#showSearch").bind("click", function () {
            if ($(this).hasClass("fa-angle-down")) {
                $(this).removeClass("fa-angle-down").addClass("fa-angle-up");
                $("#search-from").show();
            } else {
                $("#search-from").hide();
                $(this).removeClass("fa-angle-down").addClass("fa-angle-down");
            }
        });
    },

    //@入口方法
    init: function () {
        var t = this;
        t.bindEvents();
        t.initPage();
    }
}.init();